package com.example.communitypro.service.impl;

import com.example.communitypro.mapper.*;
import com.example.communitypro.pojo.*;
import com.example.communitypro.service.LabelRelationService;
import com.example.communitypro.service.UserLikesService;
import com.example.communitypro.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserLikesServiceImpl implements UserLikesService {
    @Autowired(required=false)
    private UserLikesMapper userLikesMapper;
    @Autowired(required=false)
    private CommunityInformationMapper communityInformationMapper;
    @Autowired(required=false)
    private PostMapper postMapper;
    @Autowired(required=false)
    private CommentMapper commentMapper;
    @Autowired(required = false)
    private LabelRelationMapper labelRelationMapper;
    @Autowired(required = false)
    private LabelMapper labelMapper;

    @Override
    public Result addUserLikes(UserLikes record){
        try {
            if(userLikesMapper.insertSelective(record)>0)
            {
                if (record.getLikeObjectType()==0){
                    CommunityInformation communityInformation = communityInformationMapper.selectByPrimaryKey(record.getLikeObjectNo());
                    int now = communityInformation.getCommunityLikeNumber()+1;
                    communityInformation.setCommunityLikeNumber(now);
                    communityInformationMapper.updateByPrimaryKeySelective(communityInformation);
                }
                if (record.getLikeObjectType()==1){
                    Post post = postMapper.selectByPrimaryKey(record.getLikeObjectNo());
                    int now = post.getPostLikeNumber()+1;
                    post.setPostLikeNumber(now);
                    postMapper.updateByPrimaryKeySelective(post);
                }
                if (record.getLikeObjectType()==2){
                    Comment comment = commentMapper.selectByPrimaryKey(record.getLikeObjectNo());
                    int now = comment.getCommentLikeNumber()+1;
                    comment.setCommentLikeNumber(now);
                    commentMapper.updateByPrimaryKeySelective(comment);
                }
                return Result.ok().message("新增用户点赞成功");
            }
            else{
                return Result.error().message("新增用户点赞失败");
            }
        }catch (Exception e){
            return Result.error().message("新增用户点赞失败");
        }
    }


    @Override
    public Result dropUserLikes(int UserLikesNo){
        try {
            if(userLikesMapper.selectByPrimaryKey(UserLikesNo)!=null)
            {
                UserLikes userLikes = userLikesMapper.selectByPrimaryKey(UserLikesNo);
                if (userLikes.getLikeObjectType()==0){
                    CommunityInformation communityInformation = communityInformationMapper.selectByPrimaryKey(userLikes.getLikeObjectNo());
                    int now = communityInformation.getCommunityLikeNumber()-1;
                    communityInformation.setCommunityLikeNumber(now);
                    communityInformationMapper.updateByPrimaryKeySelective(communityInformation);
                }
                if(userLikes.getLikeObjectType()==1){
                    Post post = postMapper.selectByPrimaryKey(userLikes.getLikeObjectNo());
                    int now = post.getPostLikeNumber()-1;
                    post.setPostLikeNumber(now);
                    postMapper.updateByPrimaryKeySelective(post);
                }
                if (userLikes.getLikeObjectType()==2){
                    Comment comment = commentMapper.selectByPrimaryKey(userLikes.getLikeObjectNo());
                    int now = comment.getCommentLikeNumber()-1;
                    comment.setCommentLikeNumber(now);
                    commentMapper.updateByPrimaryKeySelective(comment);
                }
                userLikesMapper.deleteByPrimaryKey(UserLikesNo);
                return Result.ok().message("删除用户点赞成功");
            }
            else{
                return Result.error().message("删除用户点赞失败");
            }
        }catch (Exception e){
            return Result.error().message("删除用户点赞失败");
        }
    }

    @Override
    public Result modifyUserLikes(UserLikes record){
        try {
            if(userLikesMapper.updateByPrimaryKeySelective(record)>0)
            {
                return Result.ok().message("修改成功");
            }
            else{
                return Result.error().message("修改失败：未找到对应记录");
            }
        }catch (DuplicateKeyException e){
            System.err.println(e);
            return Result.error().message("修改失败：已经存在对应记录");
        }
        catch (Exception e){
            System.err.println(e);
            return Result.error().message("修改失败："+e.toString());
        }
    }

    @Override
    public Result queryUserLikes(int likeId){
        try {
            UserLikes userLikes=userLikesMapper.selectByPrimaryKey(likeId);
            if(userLikes!=null)
            {
                return Result.ok().message("查找用户点赞成功").data("userLikes",userLikes);
            }
            else{
                return Result.error().message("查找用户点赞失败");
            }
        }catch (Exception e){
            return Result.error().message("查找用户点赞失败");
        }
    }

    @Override
    public Result queryCommunityLike(int user_no){
        try {
            List<CommunityInformation> communityInformations=userLikesMapper.getCommunityLike(user_no);
            if(communityInformations!=null)
            {
                return Result.ok().message("查找某用户点赞的社区成功").data("communityInformations",communityInformations);
            }
            else{
                return Result.error().message("查找某用户点赞的社区失败");
            }
        }catch (Exception e){
            return Result.error().message("查找某用户点赞的社区失败");
        }
    }

    @Override
    public Result queryPostLike(int user_no){
        try {
            List<Post> posts=userLikesMapper.getPostLike(user_no);
            if(posts!=null)
            {
                return Result.ok().message("查找某用户点赞的帖子成功").data("posts",posts);
            }
            else{
                return Result.error().message("查找某用户点赞的帖子失败");
            }
        }catch (Exception e){
            return Result.error().message("查找某用户点赞的帖子失败");
        }
    }

    @Override
    public Result queryCommentLike(int user_no){
        try {
            List<Comment> comments=userLikesMapper.getCommentLike(user_no);
            if(comments!=null)
            {
                return Result.ok().message("查找某用户点赞的评论成功").data("comments",comments);
            }
            else{
                return Result.error().message("查找某用户点赞的评论失败");
            }
        }catch (Exception e){
            return Result.error().message("查找某用户点赞的评论失败");
        }
    }

    @Override
    public Result dropPostLike(int post_no) {
        try {

            if(userLikesMapper.deletePostLike(post_no)>0)
            {

                return Result.ok().message("删除帖子所有点赞记录成功");
            }
            else{
                System.out.println("失败");
                return Result.error().message("删除帖子所有点赞记录失败");
            }
        }catch (Exception e){
            return Result.error().message("删除帖子所有点赞记录失败");
        }
    }

    @Override
    public Result dropCommentLike(int comment_no) {
        try {

            if(userLikesMapper.deleteCommentLike(comment_no)>0)
            {
                return Result.ok().message("删除评论所有点赞记录成功");
            }
            else{
                return Result.error().message("删除评论所有点赞记录失败");
            }
        }catch (Exception e){
            return Result.error().message("删除评论所有点赞记录失败");
        }
    }

    @Override
    public Result UserLikes(UserLikes record) {
        try {
            UserLikes userLikes = userLikesMapper.getUserLikes(record.getUserNo(),record.getLikeObjectType(),record.getLikeObjectNo());
            System.out.println(userLikes);
            if( userLikes != null )
            {
                if (userLikes.getLikeObjectType()==0){
                    CommunityInformation communityInformation = communityInformationMapper.selectByPrimaryKey(userLikes.getLikeObjectNo());
                    int now = communityInformation.getCommunityLikeNumber()-1;
                    communityInformation.setCommunityLikeNumber(now);
                    communityInformationMapper.updateByPrimaryKeySelective(communityInformation);
                    List<Label> labels = labelRelationMapper.selectByOb(record.getLikeObjectNo(),0);
                    int now1=0;
                    for (Label label:labels)
                    {
                        now1=label.getLabelHot()-5;
                        label.setLabelHot(now1);
                        labelMapper.updateByPrimaryKey(label);
                    }
                }
                if(userLikes.getLikeObjectType()==1){
                    Post post = postMapper.selectByPrimaryKey(userLikes.getLikeObjectNo());
                    int now = post.getPostLikeNumber()-1;
                    post.setPostLikeNumber(now);
                    postMapper.updateByPrimaryKeySelective(post);
                    int now1=0;
                    List<Label> labels = labelRelationMapper.selectByOb(record.getLikeObjectNo(),1);
                    for (Label label:labels)
                    {
                        now1=label.getLabelHot()-5;
                        label.setLabelHot(now1);
                        labelMapper.updateByPrimaryKey(label);
                    }
                }
                if (userLikes.getLikeObjectType()==2){
                    Comment comment = commentMapper.selectByPrimaryKey(userLikes.getLikeObjectNo());
                    int now = comment.getCommentLikeNumber()-1;
                    comment.setCommentLikeNumber(now);
                    commentMapper.updateByPrimaryKeySelective(comment);
                }
                userLikesMapper.deleteByPrimaryKey(userLikes.getLikeId());
                return Result.ok().message("用户删除点赞成功");
            }
            else{
                if (record.getLikeObjectType()==0){
                    CommunityInformation communityInformation = communityInformationMapper.selectByPrimaryKey(record.getLikeObjectNo());
                    int now = communityInformation.getCommunityLikeNumber()+1;
                    communityInformation.setCommunityLikeNumber(now);
                    communityInformationMapper.updateByPrimaryKeySelective(communityInformation);
                    List<Label> labels = labelRelationMapper.selectByOb(record.getLikeObjectNo(),0);
                    int now1=0;
                    for (Label label:labels)
                    {
                        now1=label.getLabelHot()+5;
                        label.setLabelHot(now1);
                        labelMapper.updateByPrimaryKey(label);
                    }
                }
                if (record.getLikeObjectType()==1){
                    Post post = postMapper.selectByPrimaryKey(record.getLikeObjectNo());
                    int now = post.getPostLikeNumber()+1;
                    post.setPostLikeNumber(now);
                    postMapper.updateByPrimaryKeySelective(post);
                    List<Label> labels = labelRelationMapper.selectByOb(record.getLikeObjectNo(),1);
                    int now1=0;
                    for (Label label:labels)
                    {
                        now1=label.getLabelHot()+5;
                        label.setLabelHot(now1);
                        labelMapper.updateByPrimaryKey(label);
                    }
                }
                if (record.getLikeObjectType()==2){
                    Comment comment = commentMapper.selectByPrimaryKey(record.getLikeObjectNo());
                    int now = comment.getCommentLikeNumber()+1;
                    comment.setCommentLikeNumber(now);
                    commentMapper.updateByPrimaryKeySelective(comment);
                }
                userLikesMapper.insertSelective(record);
                return Result.error().message("用户点赞成功");
            }
        }catch (Exception e){
            return Result.error().message("用户操作失败");
        }
    }

    @Override
    public List<Integer> findUserToOther(Integer userNo) {
        return userLikesMapper.findUserToOther(userNo);
    }

    @Override
    public List<Integer> findUserByPost(Integer like_object_no) {
        return userLikesMapper.findUserByPost(like_object_no);
    }
}
